package com.wanmait.shoppingsys.util;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.wanmait.shoppingsys.dao.CityDAO;

public class Pager {

	private int pageNum;        //当前页码
	private int pageSize=15;         //每页多少数据
	private List pageDate;       //当前页数据
	private int rowCount;         //记录个数
	private String produit;   //接受传过来的关键字
	private Integer id;   //类型ID
	private Integer cityId;
	private Integer order;  //导航栏选中   （默认，价格，人气）
	
	

	public static Pager init(HttpServletRequest request)
	{
		String orderStr = request.getParameter("order");
		String pageNumStr = request.getParameter("pageNum");
		String pageSizeStr = request.getParameter("pageSize");
		String produit =request.getParameter("serach_produit");      //接收搜索传过来的关键字
		
		int  pageNum = 1;
		
		try {
			pageNum = Integer.parseInt(pageNumStr);
		} catch (NumberFormatException e) {
		}
		
		int order = 1;
		try {
			order = Integer.parseInt(orderStr);
		} catch (NumberFormatException e) {
		}
		
		int pageSize = 15;
		try {
			pageSize = Integer.parseInt(pageSizeStr);
		} catch (NumberFormatException e) {
		}
		
		Pager pager = new Pager();
		pager.setPageNum(pageNum);
		pager.setPageSize(pageSize);	
		pager.setProduit(produit);
		pager.setOrder(order);
		request.setAttribute("pager", pager);
		return pager;
	}
	public static Pager inita(HttpServletRequest request)
	{
		String orderStr = request.getParameter("order");
		String pageNumStr = request.getParameter("pageNum");
		String pageSizeStr = request.getParameter("pageSize");
		String idd =request.getParameter("id");      //类型查找
		   Integer id = Integer.parseInt(idd);
		
		int  pageNum = 1;
		try {
			pageNum = Integer.parseInt(pageNumStr);
		} catch (NumberFormatException e) {
		}
		
		int order = 1;
		try {
			order = Integer.parseInt(orderStr);
		} catch (NumberFormatException e) {
		}
		
		int pageSize = 15;
		try {
			pageSize = Integer.parseInt(pageSizeStr);
		} catch (NumberFormatException e) {
		}
		
		Pager pager = new Pager();
		pager.setPageNum(pageNum);
		pager.setPageSize(pageSize);	
		pager.setId(id);
		pager.setOrder(order);
		request.setAttribute("pager", pager);
		return pager;
	}
	
	
	public static Pager initb(HttpServletRequest request)
	{
		String pageNumStr = request.getParameter("pageNum");
		String pageSizeStr = request.getParameter("pageSize");
		String idd =request.getParameter("id");      //用户ID查找订单
		   Integer id = Integer.parseInt(idd);
		
		int  pageNum = 1;
		try {
			pageNum = Integer.parseInt(pageNumStr);
		} catch (NumberFormatException e) {
		}
		
		int pageSize = 2;
		try {
			pageSize = Integer.parseInt(pageSizeStr);
		} catch (NumberFormatException e) {
		}
		
		Pager pager = new Pager();
		pager.setPageNum(pageNum);
		pager.setPageSize(pageSize);	
		pager.setId(id);
		request.setAttribute("pager", pager);
		return pager;
	}
	
	
	public static Pager initc(HttpServletRequest request)      //按城市
	{
		String orderStr = request.getParameter("order");
		String pageNumStr = request.getParameter("pageNum");
		String pageSizeStr = request.getParameter("pageSize");
		String city = request.getParameter("city");
		String cityIdStr = request.getParameter("cityId");
		Integer cityId = CityDAO.getDAO().getCityId(city);
		if(cityIdStr!=null) {
		cityId = Integer.parseInt(cityIdStr);
		}
		
		int  pageNum = 1;
		try {
			pageNum = Integer.parseInt(pageNumStr);
		} catch (NumberFormatException e) {
		}
		
		int order = 1;
		try {
			order = Integer.parseInt(orderStr);
		} catch (NumberFormatException e) {
		}
		
		int pageSize = 15;
		try {
			pageSize = Integer.parseInt(pageSizeStr);
		} catch (NumberFormatException e) {
		}
		
		Pager pager = new Pager();
		pager.setPageNum(pageNum);
		pager.setPageSize(pageSize);	
		pager.setCityId(cityId);
		pager.setOrder(order);
		request.setAttribute("pager", pager);
		return pager;
	}

	
	
	public int getPageCount() {
		if(rowCount<0) return 1;
		return rowCount/pageSize+(rowCount%pageSize==0?0:1);
	}
    
	
	public boolean hasPrev() {
		return pageNum>1;
	}
	
	
	public boolean hasNext() {
		return pageNum<this.getPageCount();
	}
	
	
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public List getPageDate() {
		return pageDate;
	}
	public void setPageDate(List pageDate) {
		this.pageDate = pageDate;
	}
	public int getRowCount() {
		return rowCount;
	}
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}
	
	
	public String getProduit() {
		return produit;
	}


	public void setProduit(String produit) {
		this.produit = produit;
	}
	public Integer getCityId() {
		return cityId;
	}
	public void setCityId(Integer cityId) {
		this.cityId = cityId;
	}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getOrder() {
		return order;
	}
	public void setOrder(Integer order) {
		this.order = order;
	}
}
